package com.xyy.ec.pop.api.kingdinggmp.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

    private static final Logger LOG = LoggerFactory.getLogger(DateUtils.class);

    public static final String SHORT_DATE_FORMAT_STR = "yyyy-MM-dd";
    public static final String LONG_DATE_FORMAT_STR = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期格式 精确到秒 ：yyyyMMddHHmmss
     */
    public static final String DATE_FORMDATE_TO_SECOND = "yyyyMMddHHmmss";

    public static final DateFormat SHORT_DATE_FORMAT = new SimpleDateFormat(SHORT_DATE_FORMAT_STR);
    public static final DateFormat LONG_DATE_FORMAT = new SimpleDateFormat(LONG_DATE_FORMAT_STR);
    public static final String EARLY_TIME = "00:00:00";
    public static final String LATE_TIME = "23:59:59";

    /**
     * 使用预设Format格式化Date成字符串
     *
     * @return String
     */
    public static String format(Date date) {
        return date == null ? "" : format(date, LONG_DATE_FORMAT_STR);
    }

    /**
     * 将日期类型时间转换为14位数字类型yyyyMMddHHmmss
     */
    public final static long date2Long(Date date) {
        if (date == null) return 0l;
        return Long.parseLong(new SimpleDateFormat(DateUtils.DATE_FORMDATE_TO_SECOND).format(date));
    }

    /**
     * 使用参数Format格式化Date成字符串
     *
     * @return String
     */
    public static String format(Date date, String pattern) {
        return date == null ? "" : new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 使用参数Format格式化Date成字符串
     *
     * @return String
     */
    public static String format(String inputTime, String pattern) {
        SimpleDateFormat inputDateFormat = new SimpleDateFormat(); // 使用默认格式
        Date date = null;
        try {
            date = inputDateFormat.parse(inputTime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        SimpleDateFormat outputDateFormat = new SimpleDateFormat(pattern);
        return outputDateFormat.format(date);
    }

    /**
     * 字符串解析成 java.sql.Date 日期
     *
     * @param shortDate
     * @param format
     * @return
     * @author jianguo.xu
     */
    public static java.sql.Date parserShortDate(String shortDateStr, String format) {
        if (StringUtils.isEmpty(shortDateStr)) return null;
        DateFormat dateFormate = new SimpleDateFormat(format);
        try {
            Date date = dateFormate.parse(shortDateStr);
            return new java.sql.Date(date.getTime());
        } catch (ParseException e) {
            LOG.error("parser java.sql.Date error", e);
            return null;
        }
    }

    /**
     * 字符串解析成日期
     *
     * @param dateStr
     * @param format
     * @return
     * @author jianguo.xu
     */
    public static Date parserDate(String dateStr, String format) {
        if (StringUtils.isEmpty(dateStr)) return null;
        DateFormat dateFormate = new SimpleDateFormat(format);
        try {
            Date date = dateFormate.parse(dateStr);
            return new Date(date.getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 字符串解析成日期
     *
     * @param dateStr
     * @param format
     * @return
     * @author jianguo.xu
     */
    public static Date parserDateStr(String dateStr, String format) {
        if (StringUtils.isEmpty(dateStr)) return null;
        DateFormat dateFormate = new SimpleDateFormat(format);
        try {
            Date date = dateFormate.parse(dateStr);
            return date;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据TimeUnit增加指定日期的的时间
     *
     * @param date     要增加的日期
     * @param timeUnit 增加的日历字段（只能取 DAYS 到 MILLISECONDS 之间的枚举，否则报错）
     * @param value    增加的值(当值为负数时表示减少)
     * @return
     * @author jianguo.xu
     */
    public static Date add(Date date, TimeUnit timeUnit, int value) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(calField(timeUnit), value);
        return cal.getTime();
    }

    /**
     * 根据TimeUnit设定指定日期的的时间
     *
     * @param date     要设定的日期
     * @param timeUnit 设定的日历字段（只能取 DAYS 到 MILLISECONDS 之间的枚举，否则报错）
     * @param value    设定的值(当值为负数时表示减少)
     * @return
     * @author jianguo.xu
     */
    public static Date set(Date date, TimeUnit timeUnit, int value) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(calField(timeUnit), value);
        return cal.getTime();
    }

    public static enum TimeUnit {
        YEAR, MONTH, WEEK_OF_YEAR, WEEK_OF_MONTH, DAYS, DAY_OF_MONTH, HOURS, MINUTES, SECONDS, MILLISECONDS;
    }

    private static int calField(TimeUnit timeUnit) {
        int field = 0;
        if (timeUnit == TimeUnit.YEAR) field = Calendar.YEAR;
        else if (timeUnit == TimeUnit.MONTH) field = Calendar.MONTH;
        else if (timeUnit == TimeUnit.WEEK_OF_YEAR) field = Calendar.WEEK_OF_YEAR;
        else if (timeUnit == TimeUnit.WEEK_OF_MONTH) field = Calendar.WEEK_OF_MONTH;
        else if (timeUnit == TimeUnit.DAYS) field = Calendar.DAY_OF_YEAR;
        else if (timeUnit == TimeUnit.DAY_OF_MONTH) field = Calendar.DAY_OF_MONTH;
        else if (timeUnit == TimeUnit.HOURS) field = Calendar.HOUR_OF_DAY;
        else if (timeUnit == TimeUnit.MINUTES) field = Calendar.MINUTE;
        else if (timeUnit == TimeUnit.SECONDS) field = Calendar.SECOND;
        else if (timeUnit == TimeUnit.MILLISECONDS) field = Calendar.MILLISECOND;
        else throw new RuntimeException("timeUnit error");
        return field;
    }

    /**
     * 根据TimeUnit得到指定日期的值
     *
     * @param date
     * @param timeUnit
     * @return
     * @author jianguo.xu
     */
    public static int getValue(Date date, TimeUnit timeUnit) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(calField(timeUnit));
    }

    /**
     * 根据TimeUnit清除指定的日历字段
     *
     * @param date     要清除的日期
     * @param timeUnit 清除的日历字段（只能取 DAYS 到 MILLISECONDS 之间的枚举，否则报错）
     * @return
     * @author jianguo.xu
     */
    public static Date clear(Date date, TimeUnit timeUnit) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.clear(calField(timeUnit));
        return cal.getTime();
    }

    /**
     * <br>
     * 第一个日期减去第二个日期后得到的天数</br> <br>
     * 如果减去的后的天数有不满足一整天的，则不计入天数内</br>
     *
     * @param date 被减日期
     * @param day  减去的日期
     * @return 返回减去后的天数
     */
    public static long subtractDay(Date date, Date other) {
        return subtractSecond(date, other) / (24 * 60 * 60);
    }

    /**
     * 两个日期相减得到相差的毫秒数
     *
     * @param date
     * @param other
     * @return
     */
    public static long subtractSecond(Date date, Date other) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        long dateTimeInMillis = calendar.getTimeInMillis();
        Calendar otherCalendar = Calendar.getInstance();
        otherCalendar.setTime(other);
        long otherTimeInMillis = otherCalendar.getTimeInMillis();
        return (dateTimeInMillis - otherTimeInMillis) / (1000);
    }

    /**
     * 字符串解析成 java.sql.Time 时间
     *
     * @param timeStr
     * @param timeFormat
     * @return
     * @author jianguo.xu
     */
    public static java.sql.Time parserTime(String timeStr, String timeFormat) {
        DateFormat dateFormate = new SimpleDateFormat(timeFormat);
        try {
            Date date = dateFormate.parse(timeStr);
            return new java.sql.Time(date.getTime());
        } catch (ParseException e) {
            LOG.error("parser java.sql.Time error", e);
            return null;
        }
    }

    /**
     * @param date
     * @param timeFormat
     * @return
     * @author jianguo.xu
     */
    public static Date parserDateTime(Date date, String timeFormat) {
        DateFormat dateFormate = new SimpleDateFormat(timeFormat);
        try {
            String format = dateFormate.format(date);
            Date dt = dateFormate.parse(format);
            return dt;
        } catch (ParseException e) {
            LOG.error("parser java.sql.Time error", e);
            return null;
        }
    }

    /**
     * 得到某个日期在这一天中时间最早的日期对象
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getEarlyInTheDay(Date date) {
        String dateString = SHORT_DATE_FORMAT.format(date) + " " + EARLY_TIME;
        try {
            return LONG_DATE_FORMAT.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException("parser date error.", e);
        }
    }

    /**
     * 得到某个日期在这一天中时间最晚的日期对象
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getLateInTheDay(Date date) {
        String dateString = SHORT_DATE_FORMAT.format(date) + " " + LATE_TIME;
        try {
            return LONG_DATE_FORMAT.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException("parser date error.", e);
        }
    }

    /**
     * 根据年龄计算出生日
     *
     * @param age
     * @return
     * @author jianguo.xu
     */
    public static java.sql.Date getBirthday(int age) {
        Date date = new Date();
        date = add(date, TimeUnit.YEAR, -age);
        return new java.sql.Date(date.getTime());
    }

    /**
     * 得到当前日期的毫秒数
     *
     * @return
     */
    public static long getNowTime() {
        return new Date().getTime();
    }

    /**
     * 得到指定日期所在周的最早日期(星期一)
     *
     * @param date
     * @return
     */
    public static Date getEarlyIntheWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        return getEarlyInTheDay(cal.getTime());
    }

    /**
     * 得到指定日期所在周的最晚日期(星期日)
     *
     * @param date
     * @return
     */
    public static Date getLateInTheWeek(Date date) {
        return getLateInTheDay(add(getEarlyIntheWeek(date), TimeUnit.DAYS, 6));
    }

    /**
     * 得到指定日期所在月的最早日期(1号)
     *
     * @param date
     * @return
     */
    public static Date getEarlyIntheMonth(Date date) {
        return getEarlyInTheDay(DateUtils.set(date, TimeUnit.DAY_OF_MONTH, 1));
    }

    /**
     * 得到指定日期所在月的最晚日期
     *
     * @param date
     * @return
     */
    public static Date getLateInTheMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, maxDay);
        return getLateInTheDay(cal.getTime());
    }

    /**
     * 日期格式化为 今天 昨天 前天 00：00 格式 三天前为年月日时分
     *
     * @param date    需要格式化的日期
     * @param pattern 时间格式，如：yyyy-MM-dd HH:mm:ss 或 "yyyy-MM-dd HH:mm"
     * @return 返回类型为 今天 00：00 ；昨天 00：00 ；前天 00：00 ；年-月-日 00：00
     */
    public static String showDate(Date date, String pattern) {
        String dateStr = format(date, pattern);
        //获取年月日时分的具体数字
        String year = dateStr.substring(0, 4);
        Long yearNum = Long.parseLong(year);
        int month = Integer.parseInt(dateStr.substring(5, 7));
        int day = Integer.parseInt(dateStr.substring(8, 10));
        String hour = dateStr.substring(11, 13);
        String minute = dateStr.substring(14, 16);

        long addtime = date.getTime();//参数时间的毫秒数
        long today = System.currentTimeMillis();//当前时间的毫秒数
        Date now = new Date(today);
        String nowStr = format(now, pattern);
        int nowDay = Integer.parseInt(nowStr.substring(8, 10));//获取当前时间的具体天的数字（即年月日中的日）
        String result = "";
        long l = today - addtime;//当前时间与给定时间差的毫秒数
        long days = l / (24 * 60 * 60 * 1000);//这个时间相差的天数整数，大于1天为前天的时间了，小于24小时则为昨天和今天的时间
        long hours = (l / (60 * 60 * 1000) - days * 24);//这个时间相差的减去天数的小时数
        long min = ((l / (60 * 1000)) - days * 24 * 60 - hours * 60);//
        long s = (l / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - min * 60);
        if (days > 0) {
            if (days == 1) {
                result = "前天" + " " + hour + ":" + minute;
            } else {
                result = format(date, "yyyy-MM-dd HH:mm");
            }
        } else if (hours >= 0) {
            if (day != nowDay) {
                result = "昨天" + " " + hour + ":" + minute;
            } else {
                result = "今天" + " " + hour + ":" + minute;
            }
//        } else if(min > 0){
//            if(min>0 && min<15){
//                result="刚刚";
//            } else {
//                result=min+"分 前";
//            }
//        }else {
//            result=s+"秒 前";
        }
        return result;
    }

    /**
     * 获取当前时间的字符串格式
     *
     * @return '2017-08-07 17:25:04' 这种格式的时间
     */
    public static String now() {
        return format(new Date(), "yyyy-MM-dd");
    }


    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取当天的开始时间
     * @author sing
     * @date 2018/5/21 15:13
     */
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取当天的结束时间
     * @author sing
     * @date 2018/5/21 15:13
     */
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取昨天的开始时间
     * @author sing
     * @date 2018/5/21 15:13
     */
    public static Date getBeginDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取昨天的结束时间
     * @author sing
     * @date 2018/5/21 15:13
     */
    public static Date getEndDayOfYesterDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取明天的开始时间
     * @author sing
     * @date 2018/5/21 15:13
     */
    public static Date getBeginDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, 1);

        return cal.getTime();
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取明天的结束时间
     * @author sing
     * @date 2018/5/21 15:17
     */
    public static Date getEndDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取本周的开始时间
     * @author sing
     * @date 2018/5/21 15:17
     */
    @SuppressWarnings("unused")
    public static Date getBeginDayOfWeek() {

        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取本周的结束时间
     * @author sing
     * @date 2018/5/21 15:18
     */
    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取上周的开始时间
     * @author sing
     * @date 2018/5/21 15:18
     */
    @SuppressWarnings("unused")
    public static Date getBeginDayOfLastWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return getDayStartTime(cal.getTime());
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取上周的结束时间
     * @author sing
     * @date 2018/5/21 15:18
     */
    public static Date getEndDayOfLastWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfLastWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取本月的开始时间
     * @author sing
     * @date 2018/5/21 15:20
     */
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取本月的结束时间
     * @author sing
     * @date 2018/5/21 15:20
     */
    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        return getDayEndTime(calendar.getTime());
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description:获取上月的开始时间
     * @author sing
     * @date 2018/5/21 15:20
     */
    public static Date getBeginDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取上月的结束时间
     * @author sing
     * @date 2018/5/21 15:21
     */
    public static Date getEndDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 2, day);
        return getDayEndTime(calendar.getTime());
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取本年的开始时间
     * @author sing
     * @date 2018/5/21 15:21
     */
    public static Date getBeginDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        // cal.set
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);

        return getDayStartTime(cal.getTime());
    }

    /**
     * @param
     * @return java.util.Date
     * @throws
     * @Description: 获取本年的结束时间
     * @author sing
     * @date 2018/5/21 15:21
     */
    public static Date getEndDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DATE, 31);
        return getDayEndTime(cal.getTime());
    }

    /**
     * @param d
     * @return java.sql.Timestamp
     * @throws
     * @Description: 获取某个日期的开始时间
     * @author sing
     * @date 2018/5/21 15:21
     */
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * @param d
     * @return java.sql.Timestamp
     * @throws
     * @Description: 获取某个日期的结束时间
     * @author sing
     * @date 2018/5/21 15:22
     */
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * @param
     * @return java.lang.Integer
     * @throws
     * @Description: 获取今年是哪一年
     * @author sing
     * @date 2018/5/21 15:22
     */
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    //获取本月是哪一月
    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    /**
     * @param beginDate
     * @param endDate
     * @return int
     * @throws
     * @Description: 两个日期相减得到的天数
     * @author sing
     * @date 2018/5/21 15:22
     */
    public static int getDiffDays(Date beginDate, Date endDate) {
        if (beginDate == null || endDate == null) {
            throw new IllegalArgumentException("getDiffDays param is null!");
        }

        long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24);

        int days = new Long(diff).intValue();

        return days;
    }

    /**
     * @param beginDate
     * @param endDate
     * @return long
     * @throws
     * @Description: 两个日期相减得到的毫秒数
     * @author sing
     * @date 2018/5/21 15:22
     */
    public static long dateDiff(Date beginDate, Date endDate) {
        long date1ms = beginDate.getTime();
        long date2ms = endDate.getTime();
        return date2ms - date1ms;
    }

    /**
     * @param beginDate
     * @param endDate
     * @return java.util.Date
     * @throws
     * @Description: 获取两个日期中的最大日期
     * @author sing
     * @date 2018/5/21 15:23
     */
    public static Date max(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return beginDate;
        }
        return endDate;
    }

    /**
     * @param beginDate
     * @param endDate
     * @return java.util.Date
     * @throws
     * @Description: 获取两个日期中的最小日期
     * @author sing
     * @date 2018/5/21 15:23
     */
    public static Date min(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return endDate;
        }
        return beginDate;
    }

    /**
     * @param date
     * @return java.util.Date
     * @throws
     * @Description: 返回某月该季度的第一个月
     * @author sing
     * @date 2018/5/21 15:23
     */
    public static Date getFirstSeasonDate(Date date) {
        final int[] SEASON = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int sean = SEASON[cal.get(Calendar.MONTH)];
        cal.set(Calendar.MONTH, sean * 3 - 3);
        return cal.getTime();
    }

    /**
     * @param date
     * @param i
     * @return java.util.Date
     * @throws
     * @Description: 返回某个日期下几天的日期
     * @author sing
     * @date 2018/5/21 15:23
     */
    public static Date getNextDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
        return cal.getTime();
    }

    /**
     * @param date
     * @param i
     * @return java.util.Date
     * @throws
     * @Description: 返回某个日期前几天的日期
     * @author sing
     * @date 2018/5/21 15:23
     */
    public static Date getFrontDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
        return cal.getTime();
    }

    /**
     * 获取指定时间所在月份的第一天时间
     *
     * @return
     */
    public static Date getFirstDayOfMonthByDate(Date date) {
        if (null == date) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        Calendar tempCalendar = Calendar.getInstance();
        tempCalendar.clear();
        tempCalendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
        return tempCalendar.getTime();
    }

    /**
     * 获取指定时间所在月份的最后一天时间
     *
     * @return
     */
    public static Date getEndDayOfMonthByDate(Date date) {
        if (null == date) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        Calendar tempCalendar = Calendar.getInstance();
        tempCalendar.clear();
        tempCalendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, 1, 23, 59, 59);
        tempCalendar.set(Calendar.MILLISECOND, 999);
        tempCalendar.add(Calendar.DAY_OF_MONTH, -1);
        return tempCalendar.getTime();
    }

    /**
     * 取得指定日期所在季度第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfSeasonByDate(Date date) {
        if (null == date) {
            return null;
        }
        //当前时间所在季度
        int season = getSeason(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        Calendar tempCalendar = Calendar.getInstance();
        tempCalendar.clear();
        tempCalendar.set(calendar.get(Calendar.YEAR), (season - 1) * 3, 1);
        return tempCalendar.getTime();
    }


    /**
     * 取得指定日期所在季度最后一天
     *
     * @param date
     * @return
     */
    public static Date getEndDayOfSeasonByDate(Date date) {
        if (null == date) {
            return null;
        }
        //当前时间所在季度
        int season = getSeason(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        Calendar tempCalendar = Calendar.getInstance();
        tempCalendar.clear();
        tempCalendar.set(calendar.get(Calendar.YEAR), season * 3, 1, 23, 59, 59);
        tempCalendar.set(Calendar.MILLISECOND, 999);
        tempCalendar.add(Calendar.DAY_OF_MONTH, -1);
        return tempCalendar.getTime();
    }

    /**
     * 取得指定日期所在年度第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfYearByDate(Date date) {
        if (null == date) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        Calendar tempCalendar = Calendar.getInstance();
        tempCalendar.clear();
        tempCalendar.set(calendar.get(Calendar.YEAR), Calendar.JANUARY, 1);
        return tempCalendar.getTime();
    }


    /**
     * 取得指定日期所在年度最后一天
     *
     * @param date
     * @return
     */
    public static Date getEndayOfYearByDate(Date date) {
        if (null == date) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        Calendar tempCalendar = Calendar.getInstance();
        tempCalendar.clear();
        tempCalendar.set(calendar.get(Calendar.YEAR) + 1, Calendar.JANUARY, 1, 23, 59, 59);
        tempCalendar.set(Calendar.MILLISECOND, 999);
        tempCalendar.add(Calendar.DAY_OF_MONTH, -1);
        return tempCalendar.getTime();
    }

    /**
     * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
     * 根据时间获取季度：
     *
     * @param date
     * @return
     */
    public static int getSeason(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        return (month + 1) % 3 == 0 ? (month + 1) / 3 : ((month + 1) / 3) + 1;
    }

    /**
     * 时间加一个月
     *
     * @param time
     * @return
     */
    public static String addTimeMonth(int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, month);
        return format(cal.getTime(), "yyyy-MM-dd");
    }

    /**
     * 时间加一个月
     *
     * @param time
     * @return
     */
    public static Date rollTimeMonth(Date time, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.roll(Calendar.MONTH, month);
        return cal.getTime();
    }


    /**
     * 获取当前日期是星期几<br>
     *
     * @param date
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date date) {
//        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) w = 0;
        return weekDays[w];
    }


    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 时间戳转格式
     *
     * @param seconds
     * @return
     */
    public static String timeStamp2Date(String seconds) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(new Date(Long.valueOf("-28800000")));
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date(Long.valueOf(seconds)));
    }

    public static String convertStringToDate(String input) {
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = inputFormat.parse(input);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return outputFormat.format(date);
    }
}
